﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using COM.OA.Entity;
using COM.OA.BLL;
using System.Collections.Generic;

public partial class rc_rc_read : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        COM.OA.Entity.users loginuser = Session["loginuser"] as COM.OA.Entity.users;

        if (loginuser == null)
        {
            Response.Write("<script language='javascript'>parent.document.location.href='../login.aspx'</script>");
            return;
        }
        else
        {
            int id = Int32.Parse(this.Request.QueryString["id"].ToString());


            string to_title = "";
            string notetime = "";
            string username = "";
            string dept = "";
            string sendtime = "";
            string content = "";
            int toid = 0;

            int userid = 0;
            int deptid = 0;
            string sql = "to_id='{0}'";
            sql = string.Format(sql, id);
            IList<timeorder> tlist = timeorderBLL.Select(sql);
            if (tlist.Count == 1)
            {
                foreach (timeorder t in tlist)
                {
                    toid = t.to_id;
                    to_title = t.to_title;
                    notetime = t.to_orderdate.ToString();
                    userid = t.to_u_id;
                    sendtime = t.to_sendtime.ToString();
                    content = t.to_content;
                }
            }

            string sql2 = "u_id='{0}'";
            sql2 = string.Format(sql2, userid);
            IList<users> ulist = usersBLL.Select(sql2);
            if (ulist.Count == 1)
            {
                foreach (users u in ulist)
                {
                    username = u.u_username;
                    deptid = u.u_dept_id;
                }
            }

            string sql3 = "dept_id='{0}'";
            sql3 = string.Format(sql3, deptid);
            IList<department> dlist = departmentBLL.Select(sql3);
            if (dlist.Count == 1)
            {
                foreach (department d in dlist)
                {
                    dept = d.dept_department;
                }
            }

            //给页面控件设置值
            this.lbltoid.Text = toid.ToString();
            this.lbltitle.Text = to_title;
            this.lblnotetime.Text = notetime;
            this.txtcontent.Text = content;
            this.lblusername.Text = username;
            this.lbldept.Text = dept;
            this.lblsendtime.Text = sendtime;
        }
    }
    //提交
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        int toid = Int32.Parse(this.lbltoid.Text);
        string readcontent = this.ftbReadContent.Text;
        DateTime readtime = DateTime.Parse(this.Request.Form["txtreadtime"].ToString());
        string readname=this.Request.Form["txtreadname"];

        //发送人id
        int readuserid = 0;

        string sql3 = "u_username='{0}'";
        sql3 = string.Format(sql3, readname);
        IList<users> userlist = usersBLL.Select(sql3);
        if (userlist.Count == 1)
        {
            foreach (users u in userlist)
            {
                readuserid = u.u_id;
            }
        }

        string sql = "to_id='{0}'";
        sql = string.Format(sql, toid);
        IList<timeorder> tlist = timeorderBLL.Select(sql);
        if (tlist.Count == 1)
        {
            foreach (timeorder t in tlist)
            {
                timeorder torder = new timeorder();
                torder.to_id = t.to_id;
                torder.to_orderdate = t.to_orderdate;
                torder.to_title = t.to_title;
                torder.to_content = t.to_content;
                torder.to_u_id = t.to_u_id;
                torder.to_sendtime = t.to_sendtime;
                torder.to_state = 0;
                torder.to_isread = 1;
                torder.to_readerid = readuserid;
                torder.to_readcontent = readcontent;
                torder.to_readtime = readtime;

                int flag = timeorderBLL.Update(torder);
                if (flag == 0)
                {
                    this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('批阅失败！！');</script>");
                }
                this.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('批阅成功！！');</script>");
            }
        }
    }
}
